package com.dubito.muxin.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.stereotype.Component;

/**
 * @author liliang
 * @description: Netty 服务类
 * @date 2018/11/15.
 */
@Component
public class WsServer {

    private static class SingletonWsServer {
        static final WsServer INSTANCE = new WsServer();
    }

    public static WsServer getInstance() {
        return SingletonWsServer.INSTANCE;
    }

    private EventLoopGroup parentGroup;
    private EventLoopGroup childGroup;
    private ServerBootstrap server;
    private ChannelFuture future;

    public WsServer() {
        parentGroup = new NioEventLoopGroup();
        childGroup = new NioEventLoopGroup();
        server = new ServerBootstrap();
        server.group(parentGroup, childGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new WsServerInitializer());
    }

    public void start() {
        this.future = server.bind(8088);
        System.err.println("Netty 服务 启动完毕");
    }
}
